﻿// -----------------------------------------------------------------------
// <copyright file="Attachment_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Attachment_DAL :BaseDAL
    {
        // Lấy tất cả Attachment
        public static List<Attachment> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Attachments);
            return DB.Attachments.Where(n => n.Equipment.Ship_ID == shipID).ToList();
        }
        // Lấy Attachment theo Id
        public static Attachment GetDataById(int id)
        {
            return DB.Attachments.Where(m => m.Attachments == id).FirstOrDefault();
        }
        //Thêm một Attachment
        public static bool Insert(Attachment Attachment)
        {
            try
            {
                DB.Attachments.InsertOnSubmit(Attachment);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Attachment
        public static bool Delete(Attachment Attachment)
        {
            try
            {
                DB.Attachments.DeleteOnSubmit(Attachment);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Attachment
        public static bool Update(Attachment Attachment)
        {
            try
            {
                //Attachment objAttachment = GetDataById(Attachment.Attachments);
                ////objAttachment.Code = Attachment.Code;
                ////objAttachment.Equipment_ID = Attachment.Equipment_ID;
                ////objAttachment.ContentFile = Attachment.ContentFile;
                ////objAttachment.Name = Attachment.Name;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
       
        // Kiểm tra xem Attachment Code có tồn tại chưa s
        public static bool IsExits(Attachment Attachment)
        {
            Attachment objAttachment = null;
            objAttachment = DB.Attachments.Where(m => (m.Code == Attachment.Code) &&
                (m.Attachments != Attachment.Attachments) && (m.Equipment_ID == Attachment.Equipment_ID)).FirstOrDefault();
            return objAttachment != null ? true : false;
        }
        public static List<Attachment> GetDataByEquipmentID(int id, bool isChanged)
        {
            if (isChanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Attachments);
            }
            return DB.Attachments.Where(j => j.Equipment_ID == id).ToList();
        }
      
    }
}
